#include <iostream>
#include <iterator>
#include <vector>
#include <set>
#include <utility>
#include <string>

using std::cout;
using std::endl;
using std::vector;
using std::set;
using std::pair;
using std::string;

void test0()
{
    int arr[10] = {1, 3, 5, 7, 9};
    int *parr = arr;
    cout << *parr << endl;
    parr++;

    vector<int> vec = {1, 2, 5, 7, 9, 3, 6, 2, 5};
    for(auto it = vec.begin(); it != vec.end(); ++it)
    {
        cout << *it << "  ";
    }
    cout << endl;
}

void test00()
{
    //pair只能存两个值
    /* pair<int, string> sp(1, "hello");//ok */
    pair<int, string> sp{2, "hello"};//ok
    //first可以获取pair的第一个元素
    //second可以获取pair的第二个元素
    cout << sp.first << "   " << sp.second << endl;
}

void test()
{
    auto a = 10;//ok,让编译器自动推导a的类型为int
    vector<int> vec = {1, 2, 5, 7, 9, 3, 6, 2, 5};

    //set的基本特征
    //1、存放的元素是会进行去重的，元素是唯一的，不能重复
    //2、默认情况下，元素会按照从小到大进行排序
    //3、set的底层实现是红黑树
    /* set<int> number;//1、无参的 */
    /* set<int> number(vec.begin(), vec.end()); //2、迭代器范围*/
    set<int> number = {1, 4, 9, 8, 5, 6, 4, 3, 9};//3、大括号

    for(auto it = number.begin(); it != number.end(); ++it)
    {
        cout << *it << "  ";
    }
    cout << endl;

    cout << endl << "set的查找" << endl;
    size_t cnt  = number.count(3);
    size_t cnt2  = number.count(10);
    cout << "cnt = " << cnt << endl;
    cout << "cnt2 = " << cnt2 << endl;

    cout << endl;
    auto it2 = number.find(30);
    if(it2 != number.end())
    {
        cout << "该元素存在set中 " << *it2 << endl;
    }
    else
    {
        cout << "该元素不存在set中" << endl;
    }

    cout << endl << "set的插入操作" << endl;
    /* set<int>::iterator it3; */
    /* pair<set<int>::iterator, bool> ret = number.insert(7); */
    auto ret = number.insert(7);
    if(ret.second)
    {
        cout << "插入成功 " <<   *ret.first << endl;
    }
    else
    {
        cout << "插入失败了,元素已经存在set中"<< endl;
    }
    for(auto it = number.begin(); it != number.end(); ++it)
    {
        cout << *it << "  ";
    }
    cout << endl;

    cout << endl << "set的没有下标" << endl;
    /* cout << "number[1] = " << number[1] << endl;//error */

    auto it4 = number.begin();
    /* auto it4 = --number.end(); */
    cout << "*it4 = " << *it4 << endl;
    /* *it4 = 100;//error,不允许修改 */
}

int main(int argc, char *argv[])
{
    test();
    return 0;
}

